import unittest
import paramunittest
from common import Common
from common.Log import MyLog
import readConfig as readConfig
from common import configHttp as configHttp
import requests
from common import HandleExcel
from common.constants import DATA_PATH
import json

CustomConfig_xls = Common.get_xls("CustomConfigCase.xlsx", "getCustomConfig")
localReadConfig = readConfig.ReadConfig()
localConfigHttp = configHttp.ConfigHttp()


@paramunittest.parametrized(*CustomConfig_xls)
class CustomConfig(unittest.TestCase):
    excel = DATA_PATH + '/resultData.xlsx'
    def setParameters(self, case_name, url, method, user, token, returncode):
        """
        set params
        :param case_name:
        :param url:
        :param method:
        :param user:
        :param token:
        :param returncode:
        :return:
        """
        self.case_name = str(case_name)
        self.url = str(url)
        self.method = str(method)
        self.user = str(user)
        self.token = str(token)
        self.returncode = str(returncode)
        self.response = None
        self.info = None

    def description(self):
        """

        :return:
        """
        self.case_name

    def setUp(self):
        """

        :return:
        """
        self.log = MyLog.get_log()
        self.logger = self.log.get_logger()

    def testgetCustomConfig(self):
        """
        test body
        :return:
        """
        # set url

        MyLog.get_log().logger.info(self.url)
        #localConfigHttp.set_url(self.url)
        #set header
        headers = {'Content-Type': 'application/json',
                   'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
                   'Accept-Encoding': 'gzip, deflate, br',
                   'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1',
                   'Accept-Language': 'zh-CN,zh;q=0.9',
                   'Sec-Fetch-Dest': 'document',
                   'Sec-Fetch-Mode': 'navigate',
                   'Sec-Fetch-Site': 'none'}
        # set token
        if self.token == '':
            token = Common.get_visitor_token()
        else:
            token = self.token
        # set params
        param = {"user": str(self.user), "token": str(token)}
        MyLog.get_log().logger.info(param)
        # get http
        self.response = requests.get(self.url, headers=headers, params=param)
        MyLog.get_log().logger.info(self.response.text)
        # check result
        list = []
        self.info = self.response.json()
        MyLog.get_log().logger.info(self.info)
        data = self.info.get("data")
        MyLog.get_log().logger.info(data)
        mask = data['config']
        MyLog.get_log().logger.info(mask)
        for item in mask:
            MyLog.get_log().logger.info(item)
            list.append(item['name'])
        for i in range(len(list)):
            MyLog.get_log().logger.info(list[i])
        obj = HandleExcel.HandleExcel(excel=self.excel)
        obj.write_excel("name", list)
        #self.assertEqual(self.info['returncode'], self.returncode)

    def tearDown(self):
        """

        :return:
        """
        self.log.build_case_line(self.case_name, self.info['returncode'], self.info['data'])